/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wxk.Bonus.utils;

/**
 * Jan 20, 20153:49:27 PM
 * @author William Wang
 */
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import  java.util.logging.Logger;
 import java.util.logging.Formatter ;
import java.util.logging.LogRecord ;
public class BonusLogger {
 static Logger logger= Logger.getLogger("BonusLogger");   
 private static BonusLogger global_logger=null;
 private BonusLogger()
 {
String tempDir = System.getProperty("java.io.tmpdir");
             logger.setLevel(Level.INFO); 
                ConsoleHandler consoleHandler =new ConsoleHandler();
                consoleHandler.setLevel(Level.ALL);
                consoleHandler.setFormatter(new MyLogHander()); 
                
                logger.addHandler(consoleHandler);
                try
                {
                FileHandler fileHandler = new FileHandler(tempDir+"\\BonusLogger.log");
                fileHandler.setLevel(Level.INFO);
                fileHandler.setFormatter(new MyLogHander()); 
                
                logger.addHandler(fileHandler);   
                }catch(Exception e){}     
 }
 public static BonusLogger getInstance()
 {
   if(global_logger==null)  global_logger = new BonusLogger();
  return global_logger;   
 }
public  void log(String msg)
 {
  logger.info(msg);
 }
static class MyLogHander extends Formatter {
        @Override
        public String format(LogRecord record) {
                return String.format("%s:,%s,Thread:%d,Class:%s,Method:%s,\n\t:%s\n",record.getLevel() ,
                new java.util.Date(record.getMillis() ),
                record.getThreadID() ,record.getSourceClassName() ,record.getSourceMethodName() ,
                 record.getMessage());
        } 
  }
}
